D:\a\tools.proto\tools.proto\compiler\src\compiler\util\protocols.rs
Line | Count | Source |
1 | | // Copyright (c) 2024, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use std::fmt::{Debug, Formatter}; |
30 | | use bp3d_util::index_map::{Index, IndexMap}; |
31 | | use crate::compiler::Protocol; |
32 | | use crate::compiler::util::imports::ImportSolver; |
33 | | |
34 | | #[derive(Debug)] |
35 | | pub struct Entry<M, U=()> { |
36 | | pub model: M, |
37 | | pub userdata: U |
38 | | } |
39 | | |
40 | | impl<U> Index for Entry<Protocol, U> { |
41 | | type Key = str; |
42 | | |
43 | 178 | fn index(&self) -> &Self::Key { |
44 | 178 | self.model.index() |
45 | 178 | } |
46 | | } |
47 | | |
48 | | pub struct ProtocolStore<'a, T, U> { |
49 | | map: IndexMap<Entry<Protocol, U>>, |
50 | | solver: &'a T, |
51 | | } |
52 | | |
53 | | impl<'a, T: ImportSolver, U> ProtocolStore<'a, T, U> { |
54 | 23 | pub fn new(solver: &'a T) -> Self { |
55 | 23 | Self { |
56 | 23 | map: IndexMap::new(), |
57 | 23 | solver, |
58 | 23 | } |
59 | 23 | } |
60 | | |
61 | 3 | pub fn len(&self) -> usize { |
62 | 3 | self.map.len() |
63 | 3 | } |
64 | | |
65 | 0 | pub fn is_empty(&self) -> bool { |
66 | 0 | self.map.is_empty() |
67 | 0 | } |
68 | | |
69 | 38 | pub fn insert(&mut self, entry: Entry<Protocol, U>) { |
70 | 38 | self.map.insert(entry) |
71 | 38 | } |
72 | | |
73 | 84 | pub fn get(&self, full_name: &str) -> Option<&Protocol> { |
74 | 84 | self.map.get(full_name).map(|v| &v.model78 ) |
75 | 84 | } |
76 | | |
77 | 12 | pub fn entry(&self, full_name: &str) -> Option<&Entry<Protocol, U>> { |
78 | 12 | self.map.get(full_name) |
79 | 12 | } |
80 | | |
81 | 39 | pub fn get_full_type_path(&self, protocol: &Protocol, type_name: &str) -> Option<String> { |
82 | 39 | self.solver.get_full_type_path(protocol, type_name) |
83 | 39 | } |
84 | | |
85 | 6 | pub fn iter(&self) -> impl Iterator<Item = &Protocol> { |
86 | 76 | self.map.iter().map(|v| &v.model) |
87 | 6 | } |
88 | | |
89 | 3 | pub fn entries(&self) -> impl Iterator<Item = &Entry<Protocol, U>> { |
90 | 3 | self.map.iter() |
91 | 3 | } |
92 | | } |
93 | | |
94 | | impl<T, U: Debug> Debug for ProtocolStore<'_, T, U> { |
95 | 0 | fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
96 | 0 | f.write_str("ProtocolStore { solver: ")?; |
97 | 0 | f.write_str(std::any::type_name::<T>())?; |
98 | 0 | f.write_str(", map: ")?; |
99 | 0 | self.map.fmt(f)?; |
100 | 0 | f.write_str(" }") |
101 | 0 | } |
102 | | } |